//
//  LGLViewUtil.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/8.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import Foundation
import UIKit
import WisdomHUD

class LGTViewUtil: NSObject {
    static let share = LGTViewUtil()
    
    //获取一些数量
    func getAppBadge(){
        let appDelegate:AppDelegate? = UIApplication.shared.delegate as? AppDelegate
        if appDelegate != nil{
            appDelegate?.getAppBage()
        }
    }
    
    //登录
    func loginAction(nav: UINavigationController?, json: Dictionary<String, Any>?, showMessage:Bool = true){
        WisdomHUD.dismiss()
        if json == nil || nav == nil{
            return
        }
        
        let state:Int = json?["state"] as? Int ?? 0
        let message:String = json?["message"] as? String ?? ""
        if state != 0{
            if showMessage == true{
                WisdomHUD.showError(text: message)
            }
            return
        }
        
        let data:Dictionary<String, Any>? = json?["data"] as? Dictionary<String, Any>
        if data == nil{
            return
        }
        
        let bind_type:Int = data?["bind_type"] as? Int ?? 0
        //1-绑定微信
        if bind_type == 1{
            let bindWXVC = LGTBindWXVC()
            bindWXVC.loginInfo = json
            nav?.pushViewController(bindWXVC, animated: true)
            return
        }
        //2-绑定手机号
        else if bind_type == 2{
            let bindPhoneVC = LGTBindPhoneVC()
            bindPhoneVC.loginInfo = json
            nav?.pushViewController(bindPhoneVC, animated: true)
            return
        }else{
            //登录成功
            if showMessage == true{
                WisdomHUD.showSuccess(text: message)
            }
            
            //数据存储
            LGTUserUtil.share.updateUserInfo(data: data!)
            //user_type  0 没有选择角色  1-任务发布者 2-任务接收者]
            let user_type:Int  = data?["user_type"] as? Int ?? 0
            if user_type == 0{
                //角色选择
                let userChoseVC = LGTUserChoseVC()
                nav?.pushViewController(userChoseVC, animated: true)
            }else{
                //跳转主页界面
                NotificationCenter.default.post(name: Login_Success_Note, object: nil, userInfo: nil)
            }
        }
    }
    
    //跳转指定页面
    func gotoVC(nav:UINavigationController, currentVC:UIViewController, urlStr:String, isSystem:Bool = false){
        let url:String = urlStr
        if url.count == 0 {
            return
        }
        
        //跳转发布界面
        if url.contains("lgt://tp.kaijia.com/task_release"){
            let postVC:LGTPostDetailVC = LGTPostDetailVC()
            nav.pushViewController(postVC, animated: true)
            return
        }
        
        //lgt://kaijia.com/browser?url=xxx 外部浏览器打开
        
        if url.contains("lgt://kaijia.com/browser"){
            
            let gotoUrl:URLComponents? = URLComponents.init(string: url)
            if gotoUrl == nil{
                print("error \(url)")
                return
            }
            
            var url:String = ""
            for item in gotoUrl?.queryItems ?? []{
                if item.name == url{
                    url = item.value ?? ""
                    break
                }
            }
            
            let gotoURL:URL? = URL.init(string: url)
            if gotoURL != nil{
                UIApplication.shared.open(gotoURL!, options: [:], completionHandler: nil)
            }
            return
        }
        
        //跳转到登录页面
        if url.contains("lgt://kaijia.com/login"){
            let vc:LGTMainLoginVC = LGTMainLoginVC()
            nav.pushViewController(vc, animated: true)
            return
        }
        
        // 关闭浏览器
        if url.contains("lgt://kaijia.com/close"){
            nav.popViewController(animated: true)
            return
        }

        
        //跳转到首页，相当于启动应用
        if url.contains("lgt://kaijia.com/index"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 0
            return
        }
        
        //****** 广告组 *******
        //跳转到首页
        if url.contains("lgt://tp.kaijia.com/index"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 0
            return
        }
        
        //跳转到我的页
        if url.contains("lgt://tp.kaijia.com/my"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 3
            return
        }
        
        //跳转到消息页
        if url.contains("lgt://tp.kaijia.com/message"){
            let vc:LGTMessageNoteVC = LGTMessageNoteVC()
            nav.pushViewController(vc, animated: true)
            return
        }
        
        //跳转到钱包页面
        if url.contains("lgt://tp.kaijia.com/wallet"){
            let vc:LGTWalletVC = LGTWalletVC()
            nav.pushViewController(vc, animated: true)
            return
        }
        
        //跳转任务页面详情页  lgt://tp.kaijia.com/task_detail?id=任务id 跳转到任务详情页
        if url.contains("lgt://tp.kaijia.com/task_detail"){
            self.gotoTaskDetailOrOrderDetail(url: url, type: .RWZ_Task_Detail_Type, nav: nav)
            return
        }
        
        //lgt://tp.kaijia.com/task_order_detail?id=任务订单id 跳转到任务订单详情页
        if url.contains("lgt://tp.kaijia.com/task_order_detail"){
            self.gotoTaskDetailOrOrderDetail(url: url, type: .RWZ_OrderView_Type, nav: nav)
            return
        }
        
        //跳转到任务订单页
        if url.contains("lgt://tp.kaijia.com/task_order"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 1
            return
        }
        
        // 跳转到任务页
        if url.contains("lgt://tp.kaijia.com/task"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 0
            return
        }

        
        //******* 流量主 ******
        //lgt://tr.kaijia.com/index 跳转到首页
        if url.contains("lgt://tr.kaijia.com/index"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 0
            return
        }
        
        //lgt://tr.kaijia.com/my 跳转到我的页
        if url.contains("lgt://tr.kaijia.com/my"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 3
            return
        }
        
        //lgt://tr.kaijia.com/message  跳转到消息页
        if url.contains("lgt://tr.kaijia.com/message"){
            let vc:LGTMessageNoteVC = LGTMessageNoteVC()
            nav.pushViewController(vc, animated: true)
            return
        }
        
        //lgt://tr.kaijia.com/wallet 跳转到钱包页
        if url.contains("lgt://tr.kaijia.com/wallet"){
            let vc:LGTWalletVC = LGTWalletVC()
            nav.pushViewController(vc, animated: true)
            return
        }
        
        //lgt://tr.kaijia.com/task_detail?id=任务id 跳转到任务详情页
        if url.contains("lgt://tr.kaijia.com/task_detail"){
            self.gotoTaskDetailOrOrderDetail(url: url, type: .LLZ_Task_Detail_Type, nav: nav)
            return
        }
        
        //lgt://tr.kaijia.com/task_order_detail?id=任务订单id 跳转到任务订单详情页
        if url.contains("lgt://tr.kaijia.com/task_order_detail"){
            self.gotoTaskDetailOrOrderDetail(url: url, type: .LLZ_OrderView_Type, nav: nav)
            return
        }
        
        //lgt://tr.kaijia.com/task 跳转到任务页
        if url.contains("lgt://tr.kaijia.com/task"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 1
            return
        }
        
        //lgt://tr.kaijia.com/task_order 跳转到任务订单页
        if url.contains("lgt://tr.kaijia.com/task_order"){
            nav.popToRootViewController(animated: false)
            currentVC.tabBarController?.selectedIndex = 2
            return
        }
        
        let webVC:LGTBaseWebVC = LGTBaseWebVC()
        webVC.url = url
        nav.pushViewController(webVC, animated: true)
    }
    
    func gotoTaskDetailOrOrderDetail(url:String, type:DetailType, nav:UINavigationController){
        let gotoUrl:URLComponents? = URLComponents.init(string: url)
        if gotoUrl == nil{
            return
        }
        
        var id:Int = 0
        for item in gotoUrl?.queryItems ?? []{
            if item.name == "id"{
                id = Int(item.value ?? "") ?? 0
                break
            }
        }
        
        if id == 0{
            return
        }
        
        let detailVC:LGTPostDetailVC = LGTPostDetailVC()
        detailVC.viewDetailType = type
        if type == .LLZ_OrderView_Type || type == .RWZ_OrderView_Type{
            var model:TaskOrderModel = TaskOrderModel()
            model.id = id
            detailVC.orderModel = model
        }else{
            var model:TaskModel = TaskModel()
            model.id = id
            detailVC.taskModel = model
        }
        nav.pushViewController(detailVC, animated: true)
    }
    
    func commenUrl(url:String) -> Bool{
        //跳转发布界面
        if url.contains("lgt://tp.kaijia.com/task_release"){
            return true
        }
        
        //外部浏览器
        if url.contains("lgt://kaijia.com/browser"){
            return true
        }
        
        //跳转到登录页面
        if url.contains("lgt://kaijia.com/login"){
            return true
        }
        
        // 关闭浏览器
        if url.contains("lgt://kaijia.com/close"){
            return true
        }
        
        
        //跳转到首页，相当于启动应用
        if url.contains("lgt://kaijia.com/index"){
            return true
        }
        
        //****** 广告组 *******
        //跳转到首页
        if url.contains("lgt://tp.kaijia.com/index"){
            return true
        }
        
        //跳转到我的页
        if url.contains("lgt://tp.kaijia.com/my"){
            return true
        }
        
        //跳转到消息页
        if url.contains("lgt://tp.kaijia.com/message"){
            return true
        }
        
        //跳转到钱包页面
        if url.contains("lgt://tp.kaijia.com/wallet"){
            return true
        }
        
        //跳转任务页面详情页  lgt://tp.kaijia.com/task_detail?id=任务id 跳转到任务详情页
        if url.contains("lgt://tp.kaijia.com/task_detail"){
            return true
        }
        
        //lgt://tp.kaijia.com/task_order_detail?id=任务订单id 跳转到任务订单详情页
        if url.contains("lgt://tp.kaijia.com/task_order_detail"){
            return true
        }
        
        //跳转到任务订单页
        if url.contains("lgt://tp.kaijia.com/task_order"){
            return true
        }
        
        // 跳转到任务页
        if url.contains("lgt://tp.kaijia.com/task"){
            return true
        }
        
        //******* 流量主 ******
        //lgt://tr.kaijia.com/index 跳转到首页
        if url.contains("lgt://tr.kaijia.com/index"){
            return true
        }
        
        //lgt://tr.kaijia.com/my 跳转到我的页
        if url.contains("lgt://tr.kaijia.com/my"){
            return true
        }
        
        //lgt://tr.kaijia.com/message  跳转到消息页
        if url.contains("lgt://tr.kaijia.com/message"){
            return true
        }
        
        //lgt://tr.kaijia.com/wallet 跳转到钱包页
        if url.contains("lgt://tr.kaijia.com/wallet"){
            return true
        }
        
        //lgt://tr.kaijia.com/task_detail?id=任务id 跳转到任务详情页
        if url.contains("lgt://tr.kaijia.com/task_detail"){
            return true
        }
        
        //lgt://tr.kaijia.com/task_order_detail?id=任务订单id 跳转到任务订单详情页
        if url.contains("lgt://tr.kaijia.com/task_order_detail"){
            return true
        }
        
        //lgt://tr.kaijia.com/task 跳转到任务页
        if url.contains("lgt://tr.kaijia.com/task"){
            return true
        }
        
        //lgt://tr.kaijia.com/task_order 跳转到任务订单页
        if url.contains("lgt://tr.kaijia.com/task_order"){
            return true
        }
        
        if url.contains("lgt://tr.kaijia.com"){
            return true
        }
        return false
    }
    
    
    //发布任务
    func publicGotoPayVC(submitDataInfo:Dictionary<String, Any>, stepArray:Array<Dictionary<String, Any>>, nav:UINavigationController, payType:PayType = PayType.PostPayType, fromVC:UIViewController){
        
        var submitDataInfo2 = submitDataInfo
        
        if payType == PayType.PostPayType{
            var projectName = submitDataInfo["projectName"] as? String ?? ""
            if projectName.count == 0{
                WisdomHUD.showError(text: "请输入项目名称")
                return
            }
            
            if projectName.containsEmoji == true{
//                let data:Data = projectName.data(using: String.Encoding.nonLossyASCII) ?? Data()
//                projectName = String.init(data: data, encoding: String.Encoding.utf8) ?? ""
//                submitDataInfo2["projectName"] = projectName
//                projectName = projectName.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
                //数据进行加密
                //WisdomHUD.showError(text: "项目名称请不要输入表情符号")
                //return
            }
            
            let taskRequest = submitDataInfo["taskRequest"] as? String ?? ""
            if taskRequest.count == 0{
                WisdomHUD.showError(text: "请输入任务要求")
                return
            }
            
//            if taskRequest.containsEmoji == true{
//                WisdomHUD.showError(text: "任务要求请不要输入表情符号")
//                return
//            }
            
            //做单时间
            let workTime:String = (submitDataInfo["workTime"] as? PostNameModel)?.name ?? ""
            if workTime.count == 0{
                WisdomHUD.showError(text: "请选择做单时间")
                return
            }
            
            //审核时间
            let reviewTime:String = (submitDataInfo["reviewTime"] as? PostNameModel)?.name ?? ""
            if reviewTime.count == 0{
                WisdomHUD.showError(text: "请选择审核时间")
                return
            }
            
            //做单次数
            let workTimes:String = (submitDataInfo["workTimes"] as? PostNameModel)?.name ?? ""
            if workTimes.count == 0{
                WisdomHUD.showError(text: "请选择做单次数")
                return
            }
            
            //任务单价
            let workPriceStr = submitDataInfo["workPrice"] as? String ?? ""
            if workPriceStr.count == 0{
                WisdomHUD.showError(text: "请输入任务单价")
                return
            }
            
            //任务数量
            let taskCountStr:String = submitDataInfo["taskCount"] as? String ?? ""
            if taskCountStr.count == 0{
                WisdomHUD.showError(text: "请输入任务数量")
                return
            }
            
            // 下线时间
            let offline:String = submitDataInfo["offline"]  as? String ?? ""
            if offline.count == 0{
                WisdomHUD.showError(text: "请选择下线时间时间")
                return
            }
            
            if stepArray.count == 0{
                WisdomHUD.showError(text: "请添加步骤")
                return
            }
        }
        
        // 查询多少钱
        LGTHttpUtil.httpUtil.requestData(url: User_Account_URL, param: [:], success: { (json) in
            let info:Dictionary<String, Any> = json["data"] as? Dictionary<String, Any> ?? [:]
            let amount:Int = info["amount"] as? Int ?? 0
            let payVC:LGTRWZPayVC = LGTRWZPayVC()
            payVC.modalPresentationStyle = .overCurrentContext
            payVC.payType = payType
            var submit:Dictionary<String, Any?> = submitDataInfo2
            submit["step"] = stepArray
            payVC.amount = amount
            payVC.fromNav = nav
            payVC.submitDataInfo = submit
            fromVC.present(payVC, animated: false, completion: nil)
        }) { (json) in
            
        }
    }
    
    //微信支付
    func wxPay(data:Dictionary<String, Any>){
        //判断是否安装微信
        if WXApi.isWXAppInstalled() == false{
            WisdomHUD.showError(text: "请安装微信")
            return
        }
        
        let model:WXPayModel = try! lgtDecode(WXPayModel.self, param: (data))
        let payReq:PayReq = PayReq.init()
        payReq.prepayId = model.prepayid
        payReq.openID = model.appid
        payReq.partnerId = model.partnerid
        payReq.nonceStr = model.noncestr
        payReq.timeStamp = model.timestamp!
        payReq.package = model.package
        payReq.sign = model.sign
        WXApi.send(payReq)
    }
}



//跳转地址
//通用
//lgt://kaijia.com/login 跳转到登录页面
//lgt://kaijia.com/index 跳转到首页，相当于启动应用
//lgt://kaijia.com/zone?user_id=用户id  跳转到个人空间页
//
//广告主
//lgt://tp.kaijia.com/index 跳转到首页
//lgt://tp.kaijia.com/task 跳转到任务页
//lgt://tp.kaijia.com/task_order 跳转到任务订单页
//lgt://tp.kaijia.com/my 跳转到我的页
//lgt://tp.kaijia.com/message  跳转到消息页

//lgt://tp.kaijia.com/wallet 跳转到钱包页
//lgt://tp.kaijia.com/task_detail?id=任务id 跳转到任务详情页
//lgt://tp.kaijia.com/task_order_detail?id=任务订单id 跳转到任务订单详情页
//
//流量主
//lgt://tr.kaijia.com/index 跳转到首页
//lgt://tr.kaijia.com/task 跳转到任务页
//lgt://tr.kaijia.com/task_order 跳转到任务订单页
//lgt://tr.kaijia.com/my 跳转到我的页
//lgt://tr.kaijia.com/message  跳转到消息页
//lgt://tr.kaijia.com/wallet 跳转到钱包页
//lgt://tr.kaijia.com/task_detail?id=任务id 跳转到任务详情页
//lgt://tr.kaijia.com/task_order_detail?id=任务订单id 跳转到任务订单详情页
